{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "rasterio 是一个用于处理 栅格数据（如卫星影像、DEM数字高程模型）的Python库。它基于GDAL（地理空间数据抽象库）构建，但提供了更简洁的Pythonic接口，适合高效读写、操作和分析地理栅格数据。",
   "id": "b488ed0ed9358a33"
  },
  {
   "metadata": {
    "collapsed": true
   },
   "cell_type": "code",
   "source": [
    "import os\n",
    "import rasterio\n",
    "from rasterio.features import shapes\n",
    "import geopandas as gpd\n",
    "import pandas as pd\n",
    "from tqdm import tqdm  # 用于显示进度条"
   ],
   "id": "initial_id",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置路径\n",
    "input_dir = '../data/landsat/LT51290391992229BJC00'  # 替换为实际路径\n",
    "# output_shapefile = 'landsat5_newdu_1992_multiband.shp'\n",
    "output_shapefile = '../data/shape_file/xinDu_1992_shp/xinDu_1992_shp.shp'\n",
    "\n",
    "# 选择一个波段文件进行处理（这里使用B1.TI作为示例）\n",
    "band_file = 'LT51290391992229BJC00_B1.TIF'  # 你可以选择其他波段\n",
    "\n",
    "# 读取栅格数据\n",
    "with rasterio.open(os.path.join(input_dir, band_file)) as src:\n",
    "    image = src.read(1)  # 读取第一个波段\n",
    "    transform = src.transform\n",
    "    crs = src.crs\n",
    "\n",
    "    # 将栅格转换为多边形\n",
    "    results = (\n",
    "        {'properties': {'raster_val': v}, 'geometry': s}\n",
    "        for i, (s, v) in enumerate(\n",
    "            shapes(image, transform=transform, mask=image > 0)  # 假设值>0为有效数据\n",
    "        )\n",
    "    )\n",
    "\n",
    "    # 创建GeoDataFrame\n",
    "    gdf = gpd.GeoDataFrame.from_features(list(results))\n",
    "\n",
    "    # 设置坐标系\n",
    "    if crs is None:\n",
    "        # 如果原始文件没有CRS信息，我们手动设置为WGS84 (EPSG:4326)\n",
    "        gdf.crs = \"EPSG:4326\"\n",
    "    else:\n",
    "        # 如果原始文件有CRS信息，先转换为WGS84\n",
    "        gdf = gdf.to_crs(\"EPSG:4326\")\n",
    "\n",
    "# 保存为Shapefile\n",
    "gdf.to_file(output_shapefile)\n",
    "\n",
    "print(f\"Shapefile已成功保存为: {output_shapefile}\")"
   ],
   "id": "13b3641a0a7edb63",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "59a6937d499383ad",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "95a2ade2b1ebf087",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "8648a1cc1a2d82ab",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "db522472bfef1d07",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "5c421f15d610979",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "7a6a4265be808f51",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "78508bb3656d0caf",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "e697d8aa97292aba",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
